package com.android.personal.kotlin.activity

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import com.android.personal.kotlin.R
import com.android.personal.kotlin.activity.BaseAppCompatActivity
import com.android.personal.kotlin.adapter.UserAdapter
import com.android.personal.kotlin.control.WatchedImp
import com.android.personal.kotlin.control.IssueKey
import com.android.personal.kotlin.greendao.helper.GDUserHelper
import com.android.personal.kotlin.greendao.model.UserModel
import kotlinx.android.synthetic.main.activity_login_result.*
import kotlinx.android.synthetic.main.toolbar_layout.*
import rx.Observable
import rx.Subscriber
import rx.functions.Action1
import java.util.*

/**
 * Created by zhangwentao on 2016/12/7.
 */
class LoginResultActivity : BaseAppCompatActivity() {

    var mName: String? = ""
    var mPassword: String? = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login_result)

        initViews()
    }

    fun initViews() {

        getToolbarTitle().text = "数据库查找测试"
        getSubTitle().text = "retrofit请求"

        WatchedImp.getInstance().notifyWatchers(IssueKey.DATA_CHANGED, "成功")
        mName = intent.getCharSequenceExtra(MainActivity.NAME).toString()
        mPassword = intent.getCharSequenceExtra(MainActivity.PASSWORD).toString()

        result.text = "账号:" + mName + "\n密码:" + mPassword

        toolbar_subtitle.setOnClickListener { startActivity(Intent(this, RetrofitTestActivity::class.java)) }

        var userAdapter = UserAdapter(this)
        user_list.adapter = userAdapter

        add_user.setOnClickListener {
            var userModel = UserModel()
            userModel.name = name.text.toString()
            userModel.age = age.text.toString();
            userModel.sex = sex.text.toString()
            userModel.weight = weight.text.toString()
            userModel.height = height.text.toString()
            GDUserHelper.getInstance(this).insertUser(userModel)
            name.text.clear()
            age.text.clear()
            sex.text.clear()
            weight.text.clear()
            height.text.clear()
            userAdapter.setUserModelList(GDUserHelper.getInstance(this).loadUserModel())
        }

        user_search.setOnClickListener {
            Observable.just("1").subscribe(Action1 {
                userAdapter.setUserModelList(GDUserHelper.getInstance(this).loadUserModel())
            }).unsubscribe()
        }
    }
}
